package com.ruoyi.lvlian.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 身份证信息对象 sys_id_card_info
 *
 * @author liyuxi
 * @date 2024-07-23
 */

@Data
@ToString
public class IdCardInfo implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * $column.columnComment
     */
    private String id;

    /**
     * 身份证号
     */ @NotNull(message = "合同编号不能为空")
    @ApiModelProperty(value = "身份证号")
    private String idCard;

    /**
     * 持有人姓名
     */
    @ApiModelProperty(value = "持有人姓名")
    private String name;

    /**
     * 性别
     */
    @ApiModelProperty(value = "性别")
    private String gender;

    @ApiModelProperty(value = "民族")
    private String ethnicity;

    /**
     * 姓名
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    private String birthDate;

    /**
     * 身份证地址
     */
    @ApiModelProperty(value = "身份证地址")
    private String address;

    /**
     * 居住地址
     */
    @ApiModelProperty(value = "居住地址")
    private String liveAddress;

    /**
     * 发证机关
     */
    @ApiModelProperty(value = "发证机关")
    private String issuingAuthority;

    /**
     * 身份证的签发日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "身份证的签发日期" )
    private String issueDate;

    /**
     * 身份证的有效期截止日期
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "身份证的有效期截止日期" )
    private String expiryDate;

    /**
     * 身份证正面照片
     */
    @ApiModelProperty(value = "身份证正面照片")
    private String idCardImg;

    /**
     * 身份证背面照片
     */
    @ApiModelProperty(value = "身份证背面照片")
    private String idCardBackImg;

    /**
     * 创建人
     */
    @ApiModelProperty(value = "创建人")
    private String createUser;

    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd")
    private String createDate;

    /**
     * 手机号码
     */
    @ApiModelProperty(value = "手机号码")
    private String phone;

    /**
     * 紧急联络人
     */
    @ApiModelProperty(value = "紧急联络人")
    private String emergencyPhone;

    @ApiModelProperty(value = "关联用户id")
    private Integer userId;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getIdCard() {
        return idCard;
    }

    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        if (idCard == null || idCard.length() != 18) {
            // 处理无效身份证：返回null或默认值，也可抛异常
            return null; // 或 return "0"; 根据需求调整
        }
        char genderCode = idCard.charAt(16); // 获取第17位字符
        if (genderCode < '0' || genderCode > '9') {
            return null; // 非数字字符处理
        }
        // 判断奇偶：奇数为男(1)，偶数为女(0)
        return (genderCode % 2 != 0) ? "男" : "女";
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getEthnicity() {
        return ethnicity;
    }

    public void setEthnicity(String ethnicity) {
        this.ethnicity = ethnicity;
    }

    public String getBirthDate() {
        if (idCard == null || idCard.length() != 18) {
            return birthDate;
        }

        try {
            String birthDateStr = idCard.substring(6, 14);

            // 验证日期是否有效
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            sdf.setLenient(false); // 严格模式，验证日期有效性
            Date date = sdf.parse(birthDateStr);

            // 格式化为标准日期格式
            SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
            return outputFormat.format(date);
        } catch (Exception e) {
            return birthDate; // 日期格式无效时返回原值
        }
    }

    public void setBirthDate(String birthDate) {
        this.birthDate = birthDate;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getLiveAddress() {
        return liveAddress;
    }

    public void setLiveAddress(String liveAddress) {
        this.liveAddress = liveAddress;
    }

    public String getIssuingAuthority() {
        return issuingAuthority;
    }

    public void setIssuingAuthority(String issuingAuthority) {
        this.issuingAuthority = issuingAuthority;
    }

    public String getIssueDate() {
        return issueDate;
    }

    public void setIssueDate(String issueDate) {
        this.issueDate = issueDate;
    }

    public String getExpiryDate() {
        return expiryDate;
    }

    public void setExpiryDate(String expiryDate) {
        this.expiryDate = expiryDate;
    }

    public String getIdCardImg() {
        return idCardImg;
    }

    public void setIdCardImg(String idCardImg) {
        this.idCardImg = idCardImg;
    }

    public String getIdCardBackImg() {
        return idCardBackImg;
    }

    public void setIdCardBackImg(String idCardBackImg) {
        this.idCardBackImg = idCardBackImg;
    }

    public String getCreateUser() {
        return createUser;
    }

    public void setCreateUser(String createUser) {
        this.createUser = createUser;
    }

    public String getCreateDate() {
        return createDate;
    }

    public void setCreateDate(String createDate) {
        this.createDate = createDate;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmergencyPhone() {
        return emergencyPhone;
    }

    public void setEmergencyPhone(String emergencyPhone) {
        this.emergencyPhone = emergencyPhone;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }
}
